package com.chaodev.base.controller;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.chaodev.base.base.RedisDao;
import com.chaodev.base.dto.ActivityArticleDto;
import com.chaodev.base.model.ActivityArticle;
import com.chaodev.base.model.Result;
import com.chaodev.base.model.ResultEnum;
import com.chaodev.base.model.UserInfo;
import com.chaodev.base.service.ActivityArticleService;
import com.chaodev.base.service.ILogService;
import com.chaodev.base.utils.JwtUtil;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * vcbase
 *
 * @author 95634
 * @描述 活动信息(活动资讯)
 * @date 2023/5/19 15:53
 **/
@RestController
@RequestMapping("article")
public class ActivityArticleController {
    @Autowired
    private RedisDao redisDao;

    @Autowired
    private ILogService logService;
    @Autowired
    private ActivityArticleService articleService;

    /**
     * 获取活动信息、活动结束后发表的文章列表分页
     */
    @ApiOperation(value = "获取活动信息、活动结束后发表的文章列表分页", notes = "获取活动信息、活动结束后发表的文章列表分页")
    @PostMapping(value = {"/list"})
    public Result list(@RequestBody ActivityArticleDto dto, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            PageInfo<ActivityArticle> activityNot = articleService.queryPageList(dto);
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("获取成功");
            result.setData(activityNot);
            logService.logAdd(null, "获取活动信息、活动结束后发表的文章列表分页", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取活动信息、活动结束后发表的文章列表分页", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    @ApiOperation(value = "获取活动信息、活动结束后发表的文章列表分页(无token)", notes = "获取活动信息、活动结束后发表的文章列表分页")
    @PostMapping(value = {"/listOut"})
    public Result listOut(@RequestBody ActivityArticleDto dto, HttpServletRequest request) {
        Result result = new Result();
        PageInfo<ActivityArticle> activityNot = articleService.queryPageList(dto);
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMsg("获取成功");
        result.setData(activityNot);
        logService.logAdd(null, "获取活动信息、活动结束后发表的文章列表分页", "SUCCESS", result.toString(), null, request);
        return result;
    }

    /**
     * 获取活动信息、活动结束后发表的文章对象
     */
    @ApiOperation(value = "获取活动信息、活动结束后发表的文章对象", notes = "获取活动信息、活动结束后发表的文章对象")
    @GetMapping(value = {"/getDataById"})
    public Result findById(String id, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            ActivityArticle dataById = articleService.findDataById(id);
            articleService.updateVisitNum(dataById);
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setData(dataById);
            logService.logAdd(null, "获取活动信息、活动结束后发表的文章对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取活动信息、活动结束后发表的文章对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    @ApiOperation(value = "获取活动信息、活动结束后发表的文章对象(无token)", notes = "获取活动信息、活动结束后发表的文章对象")
    @GetMapping(value = {"/getDataByIdOut"})
    public Result findByIdOut(String id, HttpServletRequest request) {
        Result result = new Result();
        ActivityArticle dataById = articleService.findDataById(id);
        articleService.updateVisitNum(dataById);
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setData(dataById);
        logService.logAdd(null, "获取活动信息、活动结束后发表的文章对象", "SUCCESS", result.toString(), null, request);
        return result;
    }

    /**
     * 保存活动信息、活动结束后发表的文章对象
     */
    @ApiOperation(value = "保存活动信息、活动结束后发表的文章对象", notes = "保存活动信息、活动结束后发表的文章对象")
    @PostMapping(value = {"/save"})
    public Result save(@RequestBody ActivityArticle article, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            if (StrUtil.isEmpty(article.getcUser())) {
                article.setcUser(userInfo.getUserId());
            }
            if (StrUtil.isEmpty(article.getSendOrgCode())) {
                article.setSendOrgCode(userInfo.getDepartId());
            }
            if (StrUtil.isEmpty(article.getSendOrgName())) {
                article.setSendOrgName(userInfo.getDepartName());
            }
            if (StrUtil.isEmpty(article.getUpdtr())) {
                article.setUpdtr(userInfo.getUserId());
            }
            articleService.saveData(article);
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("操作成功");
            result.setData(article);
            logService.logAdd(null, "保存活动信息、活动结束后发表的文章对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "保存活动信息、活动结束后发表的文章对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    /**
     * 删除活动信息、活动结束后发表的文章对象
     */
    @ApiOperation(value = "删除活动信息、活动结束后发表的文章对象", notes = "删除活动信息、活动结束后发表的文章对象")
    @GetMapping(value = {"/del"})
    public Result delete(String id, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            articleService.deleteDataById(id);
            result.setCode(ResultEnum.SUCCESS.getCode());
            logService.logAdd(null, "删除活动信息、活动结束后发表的文章对象", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "删除活动信息、活动结束后发表的文章对象", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    @PostMapping(value = "/updateVisitNum")
    @ApiOperation(value = "更新访问量")
    @ResponseBody
    @ApiImplicitParam(name = "token", paramType = "header", value = "token", dataType = "string", required = true)
    public Result updateVisitNum(@RequestBody @ApiParam("id") ActivityArticle tbAdvice, HttpServletRequest request, HttpServletResponse response) {
        Result result = new Result();
        String token = request.getHeader("token");
        Result logResult = new Result();
        UserInfo userInfo = new UserInfo();
        try {
            if (redisDao.existsKey(token)) {
                userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
                result.setCode(ResultEnum.SUCCESS.getCode());
                articleService.updateVisitNum(tbAdvice);
                result.setMsg("更新访问量成功");
            } else {
                result.setCode(-1);
                result.setMsg("登录超时");
            }
        } catch (Exception exception) {
            result.setCode(-1);
            result.setMsg("发生错误:" + exception.getMessage());
        } finally {
            BeanUtils.copyProperties(result, logResult);
            logResult.setData(null);
            String resultCode = logResult.getCode().equals(ResultEnum.SUCCESS.getCode()) ? "SUCCESS" : "ERROR";
            logService.logAdd(tbAdvice.toString(), "更新访问量", resultCode, logResult.toString(), userInfo, request);
            return result;
        }
    }

    /**
     * 获取所有咨询列表分页
     */
    @ApiOperation(value = "获取所有咨询列表分页", notes = "获取所有咨询列表分页")
    @PostMapping(value = {"/AllList"})
    public Result allList(@RequestBody ActivityArticleDto dto, HttpServletRequest request) {
        Result result = new Result();
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            PageInfo<ActivityArticle> activityNot = articleService.queryPageAllList(dto);
            result.setCode(ResultEnum.SUCCESS.getCode());
            result.setMsg("获取成功");
            result.setData(activityNot);
            logService.logAdd(null, "获取所有咨询列表分页", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取所有咨询列表分页", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }

    @ApiOperation(value = "获取所有咨询列表分页(无token)", notes = "获取所有咨询列表分页")
    @PostMapping(value = {"/AllListOut"})
    public Result allListOut(@RequestBody ActivityArticleDto dto, HttpServletRequest request) {
        Result result = new Result();
        PageInfo<ActivityArticle> activityNot = articleService.queryPageAllList(dto);
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMsg("获取成功");
        result.setData(activityNot);
        logService.logAdd(null, "获取所有咨询列表分页", "SUCCESS", result.toString(), null, request);
        return result;
    }

    /**
     * 获取我报名过的活动日程
     *
     * @param request
     * @return com.kmvc.vcbase.model.Result
     * @author zyf
     * @date 2023-09-21 17:58
     */
    @GetMapping("getMySignUpSchedule")
    @ApiOperation("获取我报名过的活动日程")
    public Result getMySignUpSchedule(@DateTimeFormat(pattern="yyyy-MM-dd")Date thisDay, HttpServletRequest request) {
        Result result = Result.success(null, "活动日常获取成功");
        String token = request.getHeader("token");
        UserInfo userInfo = null;
        if (redisDao.existsKey(token)) {
            userInfo = JwtUtil.getUserInfoFromToken(token, redisDao);
            result.setData(articleService.getMySignUpSchedule(userInfo.getUserId(),thisDay));
            logService.logAdd(null, "获取我报名过的活动日程", "SUCCESS", result.toString(), userInfo, request);
        } else {
            result.setCode(-1);
            result.setMsg("登录超时");
            logService.logAdd(null, "获取我报名过的活动日程", "ERROR", result.toString(), userInfo, request);
        }
        return result;
    }
}
